package main 

import (
	"fmt"
)

type ListNode struct {
	Val int
	Next *ListNode
}

// 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
// 1 -> 7 -> 2 -> 6 -> 3 -> 5 -> 4
//  1 2 3 4  7 6 5
func main() {
	listNums := []int{1,2,3,4,5,6,7}
	originList := &ListNode{}
	dummyHead := originList
	for _, v := range listNums {
		originList.Next = &ListNode{
			Val: v,
		}
		originList = originList.Next
	}

	//第一步，使用快慢指针，得到链表的中点
	fast, slow := dummyHead.Next, dummyHead.Next
	for fast != nil && fast.Next != nil {
		fast = fast.Next.Next
		slow = slow.Next
	}

	mid := slow 
	re := reverse(mid)
	for re != nil {
		fmt.Printf(" %v ", re.Val)
		re = re.Next
	}
	

}

//1 2 3 4
//
func reverse(head *ListNode) *ListNode {
	
	var dummyTail *ListNode
	for head != nil {
		next := head.Next		
		head.Next = dummyTail
		dummyTail = head
		head = next
	} 
	return dummyTail
}
